<template>
  <div class="app-container">
    <el-form label-width="120px">
      <el-form-item label="图片描述">
        <el-input v-model="banner.title" />
      </el-form-item>

      <el-form-item label="图片排序">
        <el-input-number v-model="banner.sort" controls-position="right" :min="0" />
      </el-form-item>

      <el-form-item label="图片类型">
        <el-select v-model="banner.type" clearable placeholder="请选择">
          <!--
            数据类型一定要和取出的json中的一致，否则没法回填
            因此，这里value使用动态绑定的值，保证其数据类型是number
          -->
          <el-option :value="1" label="pc端" />
          <el-option :value="2" label="移动端" />
        </el-select>
      </el-form-item>

      <el-form-item label="跳转路径">
        <el-input v-model="banner.linkUrl" />
      </el-form-item>

      

      <el-form-item label="图片缩略">

        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="BASE_API+'/eduoss/fileoss'"
          class="avatar-uploader">
          <img :src="banner.imageUrl" style="width: 180px;height: auto;">
        </el-upload>

      </el-form-item>
      

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'

import bannerApi from '@/api/cms/banner'

export default {
  components: { ImageCropper, PanThumb },
  data() {
    return {
      banner: {
        title: "",
        sort: 1,
        imageUrl: '/static/109951166046433599.jpg',
        type: 1,
        linkUrl: ""
      },
      BASE_API: process.env.BASE_API,
      saveBtnDisabled: false, // 保存按钮是否禁用,

    };
  },

  watch: {
    $route(to, from) {
      console.log("watch $route");
      this.init();
    },
  },

  created() {
    // console.log(this.$route.params.id);
    this.init();
  },

  methods: {
    init() {
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id;
        this.getInfo(id);
      } else {
        
      }
    },

    getInfo(bannerId){
      bannerApi.getBanner(bannerId)
        .then(res =>{
          console.log(res.data.item);
          this.banner = res.data.item
        })
    },

    handleAvatarSuccess(res, file) {
      console.log(res.data.url)
      this.banner.imageUrl = res.data.url
      console.log(this.banner);
    },

    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },

    saveOrUpdate(){
      console.log(this.banner)
      if (!this.banner.id) {
        this.addBanner()        
      }else {
        this.updateBanner()
      }
      
    },

    addBanner(){
      bannerApi.addBanner(this.banner)
        .then(res =>{
          // console.log(res);
          this.$message({
            type: 'success',
            message: '添加轮播图成功！'
          })
          this.$router.push({ path:'/cms/banner/list'})
        })
    },

    updateBanner(){
      bannerApi.updateBanner(this.banner)
        .then(res =>{
          // console.log(res);
          this.$message({
            type: 'success',
            message: '添加轮播图成功！'
          })
          this.$router.push({ path:'/cms/banner/list'})
        })
    }

  },
};
</script>

<style>
</style>